package com.ebelter.ty.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;


import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.ty.R;
import com.ebelter.ty.model.http.bean.GetAverageResponse;
import com.ebelter.ty.ui.views.LineView;
import com.ebelter.ty.utils.ScaleBaseUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class TrendHLvAdapter extends RecyclerView.Adapter<TrendHLvAdapter.ViewHolder> {



    private Context mContext;

    private List<GetAverageResponse.ResultDataBean.DataBean> dayDatas;
    private List<GetAverageResponse.ResultDataBean.DataBean> weekDatas;
    private List<GetAverageResponse.ResultDataBean.DataBean>  monthDatas;



    public void clearData() {
        if (dayDatas!=null){
            dayDatas.clear();
        }
        if (weekDatas!=null){
            weekDatas.clear();
        }
        if (monthDatas!=null){
            monthDatas.clear();
        }
    }



    //1avgWeight 2avgAR 3avgMQ 4avgBpm
    private int currentDisplayType=1;
    //10DAY 11WEEK 12MONTH
    private int currentDisplayTimeType=10;



    public void setCurrentDisplayTimeType(int currentDisplayTimeType) {
        this.currentDisplayTimeType = currentDisplayTimeType;
    }

    //设置当前显示的类型
    public void setCurrentDisplayType(int currentDisplayType) {
        this.currentDisplayType = currentDisplayType;
        calculator();
        notifyDataSetChanged();
    }
//    public void setCurrentDisplayType2(int currentDisplayType) {
//        this.currentDisplayType = currentDisplayType;
//    }




    public TrendHLvAdapter(Context context){
        this.mContext=context;
        initList();
        calculator();
    }



    private void initList(){
        if (dayDatas==null){
            dayDatas=new ArrayList<>();
        }
        if (weekDatas==null){
            weekDatas=new ArrayList<>();
        }
        if (monthDatas==null){
            monthDatas=new ArrayList<>();
        }
    }

    @NonNull
    @Override
    public TrendHLvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.trendhlv_item_ly,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TrendHLvAdapter.ViewHolder holder, int position) {

        float linePer = 0;
        String time = null;
        List<GetAverageResponse.ResultDataBean.DataBean> dataTemp = getDisplayDatas();
        if (dataTemp==null){
            linePer=0;
            time="";
        }else {
            if (currentDisplayType==1){
                linePer=maxWeight==0?0:(float) (dataTemp.get(position).getAvgWeight()/maxWeight);
            }else if (currentDisplayType==2){
                linePer=maxAR==0?0:(float) (dataTemp.get(position).getAvgAR()/maxAR);
            }else if (currentDisplayType==3){
                linePer=maxMQ==0?0:(float) (dataTemp.get(position).getAvgMQ()/maxMQ);
            }
            if (currentDisplayTimeType==10){
                time= ScaleBaseUtils.timeFatmat(dataTemp.get(position).getDays(),1);
            }else if (currentDisplayTimeType==11){
                time=ScaleBaseUtils.intNumFatmat7(dataTemp.get(position).getDays());
            }else if(currentDisplayTimeType==12){
                time=ScaleBaseUtils.monthFatmat(dataTemp.get(position).getDays());
            }
        }


        holder.lineView.setValues(linePer,time,1.0f);
    }

    @Override
    public int getItemCount() {
        if (currentDisplayTimeType==10){
            return dayDatas==null?0:dayDatas.size();
        }else if (currentDisplayTimeType==11){
            return weekDatas==null?0:weekDatas.size();
        }else if(currentDisplayTimeType==12){
            return monthDatas==null?0:monthDatas.size();
        }
        return 0;
    }

    private List<GetAverageResponse.ResultDataBean.DataBean> getDisplayDatas(){
        if (currentDisplayTimeType==10){
           return dayDatas;
        }else if (currentDisplayTimeType==11){
            return weekDatas;
        }else if(currentDisplayTimeType==12){
            return monthDatas;
        }
        return null;
    }


    //whichTime 1DAY 2WEEK 3MONTH
    public void addDatas(List<GetAverageResponse.ResultDataBean.DataBean> data, int whichTime) {
        if (whichTime==1){
            //dayDatas.addAll(0,data);
            addDatas2(dayDatas,data);
        }else if (whichTime==2){
            //weekDatas.addAll(0,data);
            addDatas2(weekDatas,data);
        }else if(whichTime==3){
          //  monthDatas.addAll(0,data);
            addDatas2(monthDatas,data);
        }
        calculator();
        notifyDataSetChanged();
    }



    private void addDatas2(List<GetAverageResponse.ResultDataBean.DataBean> bigList,List<GetAverageResponse.ResultDataBean.DataBean> subList){

        if (bigList==null||subList==null||subList.size()==0){
            return;
        }
        List<GetAverageResponse.ResultDataBean.DataBean> resutltList = new ArrayList();
        for (GetAverageResponse.ResultDataBean.DataBean subBean:subList){
            boolean isContain = isContain(bigList,subBean);
            if (!isContain){
                resutltList.add(subBean);
            }
        }
        bigList.addAll(0,resutltList);

    }


    private boolean isContain(List<GetAverageResponse.ResultDataBean.DataBean> bigList,GetAverageResponse.ResultDataBean.DataBean subBean){

        for(GetAverageResponse.ResultDataBean.DataBean bigBean:bigList){
            if (TextUtils.equals(bigBean.getDays(),subBean.getDays())){
                return true;
            }
        }
        return false;
    }


    //TYPE_WEIGHT 1
    //TYPE_BODYFAT 2
    //TYPE_MUSCLE 3
    //TIME_DAY 10
    //TIME_WEEK 11
    //TIME_MONTH 12
    public float getAvg(int TIME_TYPE,int GN_TYPE){
        List<GetAverageResponse.ResultDataBean.DataBean> data = getDataByTimeType(TIME_TYPE);
        if (data==null||data.size()==0){
            return 0;
        }
        if (0==GN_TYPE){
            return data.get(data.size()-1).getAvgWeight();
        }else if (1==GN_TYPE){
            return data.get(data.size()-1).getAvgAR();
        }else if (2==GN_TYPE){
            return data.get(data.size()-1).getAvgMQ();
        }
        return 0;
    }

    //TYPE_WEIGHT 1
    //TYPE_BODYFAT 2
    //TYPE_MUSCLE 3
    //TIME_DAY 10
    //TIME_WEEK 11
    //TIME_MONTH 12
    public float getChange(int TIME_TYPE,int GN_TYPE){
        List<GetAverageResponse.ResultDataBean.DataBean> data=getDataByTimeType(TIME_TYPE);
        if (data==null||data.size()==0||data.size()==1){
            return 0;
        }else if (data.size()>1){
            if (0==GN_TYPE){
                float f1= NumUtils.numBaoLiuWei1(ScaleBaseUtils.adapterDataFloat(data.get(data.size()-1).getAvgWeight()));
                float f2=NumUtils.numBaoLiuWei1(ScaleBaseUtils.adapterDataFloat(data.get(data.size()-2).getAvgWeight()));
                return Math.abs(f1-f2);
            }else if (1==GN_TYPE){
                float f3= NumUtils.numBaoLiuWei1(data.get(data.size()-1).getAvgAR());
                float f4=NumUtils.numBaoLiuWei1(data.get(data.size()-2).getAvgAR());
                return Math.abs(f3-f4);
            }else if (2==GN_TYPE){
                float f5= NumUtils.numBaoLiuWei1(ScaleBaseUtils.adapterDataFloat(data.get(data.size()-1).getAvgMQ()));
                float f6=NumUtils.numBaoLiuWei1(ScaleBaseUtils.adapterDataFloat(data.get(data.size()-2).getAvgMQ()));
                return Math.abs(f5-f6);
            }
        }

        return 0;
    }

    private List<GetAverageResponse.ResultDataBean.DataBean> getDataByTimeType(int TIME_TYPE){
        List<GetAverageResponse.ResultDataBean.DataBean> data=null;
        if (TIME_TYPE==10){
            data=dayDatas;
        }else if (TIME_TYPE==11){
            data=weekDatas;
        }else if (TIME_TYPE==12){
            data=monthDatas;
        }
        return data;
    }



    //ViewHolder，需要继承RecyclerView.ViewHolder
    static class ViewHolder extends RecyclerView.ViewHolder{
        private LineView lineView;
        ViewHolder(View itemView) {
            super(itemView);
            lineView = itemView.findViewById(R.id.trendhlv_item_lineview);
        }
    }

    public float maxWeight;
    public float maxAR;
    public float maxMQ;
    //计算最大值
    public void calculator(){
        List<GetAverageResponse.ResultDataBean.DataBean> datas = getDisplayDatas();;


        if (datas==null||datas.size()==0){
            maxWeight=0;
            maxAR=0;
            maxMQ=0;

        }else if (datas.size()==1){
            maxWeight= (float) datas.get(0).getAvgWeight();
            maxAR= (float) datas.get(0).getAvgAR();
            maxMQ= (float) datas.get(0).getAvgMQ();
        }else {
            GetAverageResponse.ResultDataBean.DataBean maxWeight1 = Collections.max(datas, new Comparator<GetAverageResponse.ResultDataBean.DataBean>() {
                @Override
                public int compare(GetAverageResponse.ResultDataBean.DataBean o1, GetAverageResponse.ResultDataBean.DataBean o2) {
                    if (o1.getAvgWeight()<o2.getAvgWeight()){
                        return -1;
                    }else if (o1.getAvgWeight()>o2.getAvgWeight()){
                        return 1;
                    }else {
                        return 0;
                    }

                }
            });
            GetAverageResponse.ResultDataBean.DataBean maxAR1= Collections.max(datas, new Comparator<GetAverageResponse.ResultDataBean.DataBean>() {
                @Override
                public int compare(GetAverageResponse.ResultDataBean.DataBean o1, GetAverageResponse.ResultDataBean.DataBean o2) {
                    if (o1.getAvgAR()<o2.getAvgAR()){
                        return -1;
                    }else if (o1.getAvgAR()>o2.getAvgAR()){
                        return 1;
                    }else {
                        return 0;
                    }

                }
            });

            GetAverageResponse.ResultDataBean.DataBean maxMQ1= Collections.max(datas, new Comparator<GetAverageResponse.ResultDataBean.DataBean>() {
                @Override
                public int compare(GetAverageResponse.ResultDataBean.DataBean o1, GetAverageResponse.ResultDataBean.DataBean o2) {
                    if (o1.getAvgMQ()<o2.getAvgMQ()){
                        return -1;
                    }else if (o1.getAvgMQ()>o2.getAvgMQ()){
                        return 1;
                    }else {
                        return 0;
                    }

                }
            });

            maxWeight=  maxWeight1.getAvgWeight();
            maxAR= maxAR1.getAvgAR();
            maxMQ=  maxMQ1.getAvgMQ();

        }
        maxWeight= (float) (maxWeight*1.2);
        maxAR= (float) (maxAR*1.2);
        maxMQ= (float) (maxMQ*1.2);

    }


}
